var detialId = getQueryString("detialId");

//查看评论详情
function itemsDetialFun(){
	$.ajax({
		url: API_URL+'comment/main?curriculumid='+detialId,
		type: 'GET',
		dataType: "JSON",
		data:'',
		async: false,
		success: function (obj){
			if (obj.status.code == 0) {
					var myData = obj.basedata.data;
					var Str = '';
					for(var i=0; i < myData.length; i++){
						var agentItem =myData[i];
						if(agentItem.openid_icon == null || agentItem.openid_icon == ''){
							agentItem.openid_icon ='../img/default.png';
						}
						var agent_time = new Date(agentItem.createtime).format("MM-dd hh:mm");
						Str +='<div class="items clear">'+
										'<a href="personal_page.html?openid='+agentItem.openid+'&currentopenid='+openid+'"><img src=\"'+agentItem.openid_icon+'\" alt=""></a>'+
										'<div class="info_head"><h3 class="talk_name">'+agentItem.openid_name+'</h3>'+
										'<p class="time">'+agent_time+'</p></div>'+
										'<p class="clear">'+agentItem.content+'</p>'+
									'</div>';
					}					
				}
		if(Str == ''){
				Str ='<div style="width:100%; height:100%;text-align:center;height:185px;background:#fafafa;"><img src="../img/icon-sofa.png" style="width: 55px;height: 42px;margin: 64px auto 14px;"><p style="font-size: 13px;color: #b0b0b0;">还没有评论，快来评论吧！</p></div>';
		}else{
			Str = Str;
		}
		$('.palce.talk').html(Str);
		},
		error: function (data){
			console.log("服务器异常!");
		}
	});
}
//查看列表详情
function itemsFun(){
	$.ajax({
		url: API_URL+'curriculum/findCurriculum?currid='+detialId+'&openid='+openid,
		type: 'GET',
		dataType: "JSON",
		data:'',
		async: false,
		success: function (obj){
			if (obj.status.code == 0) {
				var myData = obj.data[0];
				var Str = '';
				if(myData.agent_icon == null || myData.agent_icon == ''){
					myData.agent_icon ='../img/default.png';
				}
				var welldone ='';
				if(myData.pa_state == 1){
					welldone = ' welldone';
				}
				Str +='<div class="head_content">'+
							'<img src=\"'+myData.agent_icon+'\" alt="">'+
							'<h3>'+myData.agent_name;
				if(myData.agent_category == 2){
					Str += '<i class=""></i>';
				}else{
					Str += '<i class="brand_icon"></i>';
				}
						
				Str += '</h3><p>'+myData.createtime_str+'</p>'+
						'</div>';
				if(myData.type == 'photo'){
						Str += '<p class="article clear">'+myData.content+'</p>';
							if(myData.photo!=null && myData.photo!=''){
								Str += '<img class="detials_img" src=\"'+myData.photo+'\" alt=""/>';
							}
					}else if(myData.type == 'video'){
						Str += '<p class="article clear">'+myData.title+'</p>'+
							'<iframe height="140" width="181" src=\"'+myData.url+'\" frameborder=0 allowfullscreen></iframe>'+
							'<ul class="radiolabel_content">'+
								'<li><span>'+myData.channelname+'</span></li>'+
								'<li><span>'+myData.curriculumtypename+'</span></li>'+
								'<li><span>'+myData.standardname+'</span></li>'+
							'</ul>';
							
							
						/*
								'<video height="140" width="181"  controls="controls" preload="none" webkit-playsinline="webkit-playsinline">'+
					                '<source src=\"'+myData.url+'\" type="video/mp4" preload="auto">'+
					             '</video>';
						*/
					}else{
						//课件和案例详情
						Str += '<p class="article clear">'+myData.content+'</p>';
						Str += '<a href="'+myData.url+'"><div class="pdf_content">' +
								 	'<img class="pdf_img" src="../img/icon-pdf22.png" alt="">' + 
								 	'<h3>'+myData.title+'</h3>'+
								 '</div></a>';
					}

				if (myData.pa_state == 1) {
					$('.well_btn').addClass('welldone').html('<i class="well"></i>已赞');
				}else{
					$('.well_btn').removeClass('welldone').html('<i class="well"></i>赞');
				}
				
			}
		$('.title_say.talk_num em').text(myData.commentcount);
		$('.show_window span:first-child em').text(myData.forwardcount);
		$('.show_window span:last-child').addClass(welldone);
		$('.show_window span:last-child em').text(myData.praisecount);
		$('.show_window span:nth-child(2) em').text(myData.commentcount);
		$('.main_content.new').html(Str);
		},
		error: function (data){
			console.log("服务器异常!");
		}
	});

	$('.head_content').on('click',function(){
		var agent_openId = getQueryString("agentopenid");
		window.location = "personal_page.html?openid="+agent_openId+'&currentopenid='+openid;
	});

	
}
//点赞
function wellDoneFun(){
	$.ajax({
		url: API_URL+'curriculum/praise?openid='+openid+'&curriculumid='+detialId,
		type: 'post',
		dataType: "JSON",
		data:'',
		async: false,
		success: function (obj){
			if (obj.status.code == 0) {
					var wellnum = parseInt($('.wellshow em').text());
					console.log(wellnum);
					$('.wellshow em').text(wellnum+1);
					$('.wellshow').addClass('welldone');
					$('.well_btn').addClass('welldone').html('<i class="well"></i>已赞');
				}
		},
		error: function (data){
			console.log("服务器异常!");
		}
	});
}
//取消赞
function cancelFun(){
	$.ajax({
		url: API_URL+'curriculum/cancelpraise?openid='+openid+'&curriculumid='+detialId,
		type: 'post',
		dataType: "JSON",
		data:'',
		async: false,
		success: function (obj){
			if (obj.status.code == 0) {
					var wellnum = parseInt($('.wellshow em').text());
					$('.wellshow em').text(wellnum-1);
					$('.wellshow').removeClass('welldone');
					$('.well_btn').removeClass('welldone').html('<i class="well"></i>赞');
				}
		},
		error: function (data){
			console.log("服务器异常!");
		}
	});
}

//分享
function shareFun(){
	$.ajax({
		url: API_URL+'curriculum/forward?openid='+openid+'&curriculumid='+detialId,
		type: 'post',
		dataType: "JSON",
		data:'',
		async: false,
		success: function (obj){
			if (obj.status.code == 0) {
					console.log('分享成功！')
				}
		},
		error: function (data){
			console.log("服务器异常!");
		}
	});
}

//评论
function talkFun(){
	var fenObj = $('.store li');
	var number = -1;
	for(var i=0; i<fenObj.length; i++){
		if($(fenObj).hasClass('on')){
			number++;
		}
	}
	var content = $('#discuss').val();
	
	if(content.length<5){
		alert('评论必须大于四个字哦！');
		return;
	}

	$.ajax({
		url: API_URL+'curriculum/comment?openid='+openid+'&curriculumid='+detialId,
		type: 'post',
		dataType: "JSON",
		data:{
			content: content,
			score: number
		},
		async: false,
		success: function (obj){
			if (obj.status.code == 0) {
					alert('评论成功！');
					$('.talk_mask').hide();
					itemsDetialFun();
				}
		},
		error: function (data){
			console.log("服务器异常!");
		}
	});
}